<?php

namespace Mlym\CodeGeneration\ControllerGeneration\Method;


use Mlym\CodeGeneration\Middleware\Bean\View\ViewColumnBean;

class GetList extends MethodAbstract
{

    protected $methodName = 'getList';
    protected $methodDescription = '获取数据列表';
    protected $methodAllow = 'GET';


    function addMethodBody()
    {
        $method = $this->method;
        $modelName = $this->getModelName();
        $methodBody = '';
        /**
         * Page注解
         */
        $method->addComment("@Param(name=\"pageNo\", from={GET}, alias=\"页数\", optional=\"\", integer=\"\", defaultValue=\"1\")");
        $method->addComment("@Param(name=\"pageSize\", from={GET}, alias=\"每页总数\", optional=\"\", integer=\"\", defaultValue=\"20\")");

        foreach ($this->controllerConfig->getMiddlewareData()->getView() as $viewColumn) {
            if ($viewColumn instanceof ViewColumnBean) {
                $param = $this->buildSearchColumnParam($viewColumn);
                $this->addColumnComment($param);
                $this->buildWhere($viewColumn, $methodBody);
            }
        }

        $methodBody = <<<Body
\$param = ContextManager::getInstance()->get('param');
\$pageNo = (int)(\$param['pageNo'] ?? 1);
\$pageSize = (int)(\$param['pageSize'] ?? 20);
\$model = new {$modelName}();
{$methodBody}
\$data = \$model->getList(\$pageNo, \$pageSize);
\$this->writeJson(Status::CODE_OK, \$data, '操作成功');
Body;
        $method->setBody($methodBody);
    }

    function addComment()
    {
        $this->method->addComment("@throws \Throwable");
    }
}
